package org.bambrikii.photobank.web.events.beans;

import java.util.List;

import org.bambrikii.photobank.business.managers.CategoryManager;
import org.bambrikii.photobank.business.managers.EventManager;
import org.bambrikii.photobank.business.managers.FileManager;
import org.bambrikii.photobank.model.Category;
import org.bambrikii.photobank.model.CategoryTypeEnum;
import org.bambrikii.photobank.model.Event;
import org.bambrikii.photobank.model.File;
import org.bambrikii.site.base.business.managers.ConfigurationManager;
import org.bambrikii.site.base.business.managers.core.ManagerException;
import org.bambrikii.site.base.model.Pager;
import org.bambrikii.site.base.struts2.core.WebException;
import org.bambrikii.site.base.struts2.core.crud.BaseCrudBean;

public class EventBean extends BaseCrudBean<Event, EventManager> {
	private static final String BRIEF_EVENTS_LIST_MAX_RECORDS_COUNT = ".ru.psfotos.events.briefListMaxRecordsCount";

	private EventManager eventManager;
	private CategoryManager categoryManager;
	private FileManager fileManager;
	private ConfigurationManager configurationManager;

	public EventManager getManager() {
		return eventManager;
	}

	public EventBean(EventManager eventManager, CategoryManager categoryManager, FileManager fileManager,
			ConfigurationManager configurationManager) {
		this.eventManager = eventManager;
		this.categoryManager = categoryManager;
		this.fileManager = fileManager;
		this.configurationManager = configurationManager;
	}

	public Pager<Event> getEvents(Category category, int cols, int rows, int currPage) throws Exception {
		return eventManager.getEventPager(category, new Pager<Event>(cols, rows, currPage));
	}

	public Category getCategory(Integer categoryId) throws WebException {
		try {
			return categoryManager.getById(categoryId);
		} catch (ManagerException ex) {
			throw new WebException("CategoryManager failed!", ex);
		}
	}

	public Event getEvent(Integer id) throws WebException {
		try {
			return eventManager.getById(id);
		} catch (ManagerException ex) {
			throw new WebException("EventManager failed", ex);
		}
	}

	public Pager<File> getFiles(Event event, int currPage) throws ManagerException {
		return fileManager.getFilesPager(event, currPage);
	}

	public List<File> getFiles(Event event) throws ManagerException {
		return fileManager.getFiles(event, "modificationTime DESC");
	}

	public List<Category> getCategories(CategoryTypeEnum categoryType) throws WebException {
		return categoryManager.getList(null, null, categoryType);
	}

	public List<Event> getEventBriefList(Category categ) throws ManagerException {
		Integer mx = Integer.parseInt(configurationManager.getCacheConfigValue(BRIEF_EVENTS_LIST_MAX_RECORDS_COUNT));
		return getManager().getEventsList(categ, "insertionTime DESC, modificationTime DESC", mx);
	}
}
